package br.nextfm.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.nextfm.dao.ArtistaDao;
import br.nextfm.model.Artista;

public class AtualizaArtista extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		ArtistaDao dao = new ArtistaDao();
		Artista artista = dao.get(id);
		
		request.setAttribute("artistaSelecionado", artista);
		request.setAttribute("editar", true);
		request.setAttribute("artistas", dao.getAll());
		request.getRequestDispatcher("artista.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String nome = request.getParameter("nome");
		String descricao = request.getParameter("descricao");
		String tags = request.getParameter("tags");
		String imagem = request.getParameter("foto");
		
		int id = Integer.parseInt(request.getParameter("id"));
		
		ArtistaDao dao = new ArtistaDao();
		
		Artista artista = dao.get(id);
		
		artista.setDescricao(descricao);
		artista.setImagem(imagem);
		artista.setNome(nome);
		artista.setTags(tags);
		
		try {

			dao.update(artista);

			request.setAttribute("artistas",dao.getAll());
			request.getRequestDispatcher("artista.jsp").forward(request, response);

		}

		catch (Exception e) {

			request.setAttribute("artistas",dao.getAll());
			request.getRequestDispatcher("artista.jsp").forward(request, response);

		}
		
	}

}
